function banner_select_image(oObj, iMediaId) {

	if (oBanner.imageSel !== null) {
		$.post('/admin/media_ajax/get_media_props', { iMediaId : iMediaId }, function(data) {
		
			var sImgSrc = '/assets/media/thumbs/' + data.media_subfolder + '/' + data.media_id + '.' + data.extension_ext;
		
			$(oBanner.imageSel).attr('src', sImgSrc).prev().val(data.media_id);
		
			oBanner.imageSel = null;
			
			$('#overlay_id').remove();
			
		}, 'json');
	}
	
}


function check_banner_images_length() {
	if ($('.banner_image_main_cont').length === 0) {
		$('#banner_no_banners').show();
		$('#banner_images_cont').hide();
	} else {
		$('#banner_no_banners').hide();
		$('#banner_images_cont').show();
	}
	
}



function set_banner_text_sortable(oObj) {
	<?php
	
	if ($this->config->item('banner_images_text_count') > 1)
	{
		?>
		var oOptions = {
			tolerance : 'pointer',
			opacity : 0.5
		};
			
		if (!oObj) {
			oObj = '.banner_image_text';
		}
		
		$(oObj).sortable(oOptions);
		<?php
		
	}
	
	?>
}




$(document).ready(
	function() {
	
		check_banner_images_length();
		
		$('#banner_images_cont').sortable({
			handle : '.banner_image_handle',
			tolerance : 'pointer',
			scroll : true,
			opacity : 0.5
		});
		
		set_banner_text_sortable();
		
		if ($('#banner_images_cont').html() !== '') {
			$('#banner_images_cont').show();
		}
		
		
		$('.text_expanded').live('click', function(e) {
			if ($(this).attr('checked') === true) {
				$(this).parent().parent().parent().parent().find('.text_expanded:checked').removeAttr('checked');
				$(this).attr('checked', 'checked');
			}
		});
		
		
		$('.banner_image_remove').live('click', function(e) {
			$(this).parent().slideUp(500, function() {
				$(this).remove();
				check_banner_images_length();
				
				
			});
		});
		
		
		$('.banner_image_image_tab').live('click', function(e) {
			var oObj = $(this).parent().next().children('.banner_image_image');
			
			if ($(oObj).css('display') === 'none') {
				$(this).next().removeClass('banner_image_tab_selected');
				$(oObj).show();
				$(this).addClass('banner_image_tab_selected');
				$(oObj).next().hide();
			}
		});
		
		$('.banner_image_text_tab').live('click', function(e) {
			var oObj = $(this).parent().next().children('.banner_image_text');
			
			if ($(oObj).css('display') === 'none') {
				$(this).prev().removeClass('banner_image_tab_selected');
				$(oObj).show();
				$(this).addClass('banner_image_tab_selected');
				$(oObj).prev().hide();
			}
		});
		
		
		$('#banner_add_new').bind('click', function(e) {
			
			$.post('/admin/banners_ajax/get_banner_image_template', {}, function(data) {
				$('#banner_images_cont').append(data.sContent);
				
				if ($('.banner_image_main_cont').length === 0) {
					$('#banner_images_cont').hide();
				} else {
					$('#banner_images_cont').show();
				}
				
				set_banner_text_sortable($('.banner_image_text:last'));
				
			}, 'json');
			
		});
		
		
		$('.banner_select_img').live('click', function(e) {
			oBanner.imageSel = $(this).prev();
			
			create_popup('iframe_id', 'overlay_id', '962', '500', 'Test title', '/admin/media/popup/banner_select_image', function(oObj) {
				$('#overlay_id').show();
			});
		});
		
		
		
		$('#banner_save_button').bind('click', function(e) {
	
			
			// get the banner title
			var sBannerTitle = $('#banner_title').val();
			var sVersionActive = (($('#version_active:checked').length === 1) ? 'Y' : 'N');
			
			if (sBannerTitle === '') {
				alert('missing banner title'); //TODO: add a nice alert and change to $aMessages var
				return;
			}
			
			var iImgCount = 0;
			
			// check to see if each banner has an image
			$('.banner_image_image').children('img').each(
				function() {
					if ($(this).attr('src') === '') {
						iImgCount++;
					}
				}
			);
			
			
			if (iImgCount > 0) {
				alert('Select an image for each banner');//TODO: add a nice alert and change to $aMessages var
				return;
			}
			
			
			// firstly, get the new version number
			$.post('/admin/banners_ajax/get_version_number', { iVersionId : oBanner.version_id, sBannerTitle : sBannerTitle, sVersionActive : sVersionActive }, function(data) {
				
				var iVersionId = data.iVersionId;
				
				// now recursively, add each image, and then each relevant text blocks
				
							
				$('.banner_image_cont').each(
					function(iImagePos) {
						// set our global oObj
						var oObj = this;
			
						var oParams = { 
							iVersionId : iVersionId,
							iMediaId : $(oObj).contents().find('input[name="media_id[]"]').val(),
							iImagePos : iImagePos
						};
						
						$.post('/admin/banners_ajax/add_images_to_version', oParams, function(data) {
							var iImageId = data.iImageId;
							
							// now recursively, add each text block
							
							var iTextPos = 0;
							
							$(oObj).contents().find('.banner_image_text_cont').each(
								function(iTextPos) {
									 var oTextParams = {
									 	iImageId : iImageId,
										sTextHeader : $(this).find('.text_header').val(),
										sTextContent : $(this).find('.text_content').val(),
										iTextPos : iTextPos,
										sTextExpanded : (($(this).find('.text_expanded:checked').length === 1) ? 'Y' : 'N')
										/*
										// TODO: get the user to select a page to link to
										,
										iPageId : 0
										*/
									 }
									 
									 $.post('/admin/banners_ajax/add_text_to_image', oTextParams, function(data) {
									 
									 }, 'json');
								}
							);
							
							
							
						}, 'json');
						

					}
				);
						
			
			}, 'json');
		

		});

	}
);